package free.order.common.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
/**
 * 
 * 
 * @author stan
 * 
 */
public abstract class BaseAction extends ActionSupport {

	private static final Log log = LogFactory
			.getLog(BaseAction.class.getName());

	private static final long serialVersionUID = 1L;

	/**
	 * 增加的常量的字符常量,用于webwork配置
	 */
	protected final String DO_ADD = "doAdd";

	/**
	 * 编辑的字符常量,用于webwork配置
	 */
	protected final String DO_EDIT = "doEdit";

	/**
	 * 删除的字符常量,用于webwork配置
	 */
	protected final String DO_DELETE = "doDelete";

	/**
	 * 查询的字符常量,用于webwork配置
	 */
	protected final String DO_QUERY = "doQuery";

	/** 操作结果信息 */
	private String message = "";

	/** 操作结果信息代码 */
	private String messageCode = "";

	/** 操作异常信息 */
	private String error = "";

	/**
	 * Action转到jsp的模式,编辑或查看
	 */
	private String actionModel = "";

	/**
	 * 编辑模式
	 */
	public final static String MODEL_EDIT = "edit";

	/**
	 * 查看模式
	 */
	public final static String MODEL_VIEW = "view";

	/**
	 * 获取到request
	 * 
	 * @return HttpServletRequest
	 * @author hezhenbo
	 */
	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获取到response
	 * 
	 * @return HttpServletResponse
	 * @author hezhenbo
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获取到session
	 * 
	 * @return HttpSession
	 * @author hezhenbo
	 */
	protected HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	/**
	 * 用javascript弹出提示信息
	 * 
	 * @param msg
	 *            显示的信息
	 * @return String
	 * @throws IOException
	 * @author hezhenbo
	 */
	protected String jsAlert(String msg) {
		try {
//			Common.noCache(getRequest(), getResponse());
			this.getResponse().setContentType("text/html;charset=utf-8");
			PrintWriter out = this.getResponse().getWriter();
			out.print("<script language='javascript'>alert('" + msg
					+ "');</script>");
			out.close();
			return null;
		} catch (Exception e) {
			log.error("jsAlert(" + msg + ") error", e);
			return null;
		}
	}

	/**
	 * 用javascript弹出提示信息并关闭窗口
	 * 
	 * @param msg
	 *            显示的信息
	 * @return String
	 * @throws IOException
	 * @author hezhenbo
	 */
	protected String jsAlertAndClose(String msg) {
		try {
//			Common.noCache(getRequest(), getResponse());
			this.getResponse().setContentType("text/html;charset=utf-8");
			PrintWriter out = this.getResponse().getWriter();
			out.print("<script language='javascript'>alert('" + msg
					+ "');window.close();</script>");
			out.close();
			return null;
		} catch (Exception e) {
			log.error("jsAlertAndClose(" + msg + ") error", e);
			return null;
		}
	}

	/**
	 * 记录日志
	 * 
	 * @param logMsg
	 * @return false--记录失败,true--记录成功
	 * @author hezhenbo
	 */
	protected boolean log(String logMsg) {
		return false;
	}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getActionModel() {
		return actionModel;
	}

	public void setActionModel(String actionModel) {
		this.actionModel = actionModel;
	}

	public String getMessageCode() {
		return messageCode;
	}

	public void setMessageCode(String messageCode) {
		this.messageCode = messageCode;
	}
}
